package com.xb.dao;

import com.xb.entity.User;
import org.apache.ibatis.annotations.Update;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Optional;

@Transactional
public interface UserDao extends JpaRepository<User, Long> {

    User findByEmail(String email);

    User findByUsername(String username);

    Page<User> findByUsernameLike(String name, Pageable of);

    @Query("update User u set u.password = ?2 where u.email=?1")
    @Modifying
    void updatePassword(String email, String password);

    @Query("select uf.userFocusId from UserFocus uf where uf.userId=?1")
    List<Long> findFocusByUserId(Long userId);

    @Query(value = "insert into userfocus values(?1,?2)", nativeQuery = true)
    @Modifying
    void insertFocus(Long userId, Long focusId);

    @Query(value = "delete from UserFocus uf where uf.userId=?1 and uf.userFocusId=?2")
    @Modifying
    void deleteFocus(Long userId, Long focusId);

    @Query("update User set pic=?2 where id=?1")
    @Modifying
    void updatePicUrl(Long id, String url);

    @Query("select u.isSecret from User u where u.id=?1")
    String findByIsSecret(Long id);

    Optional<User> findById(Long id);

    @Query("update User set look=?2 where id=?1")
    @Modifying
    void updateLook(Long userId, Long look);

    @Query("select u from UserFocus uf left join User u on uf.userFocusId=u.id where uf.userId=?1")
    Page<User> findMyFocus(Long userId, PageRequest of);

    List<User> findByDeptId(Long deptId);

    User findByWxOpenid(String openid);
}
